/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2021-01-05
*/
package com.rzico.article.controller.member;

import com.aliyuncs.AcsErrorUnmarshaller;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.article.controller.ArticleReviewController;
import com.rzico.article.entity.ArticleReview;
import com.rzico.article.service.ArticleReviewService;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.MemberService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.exception.CustomException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 笔记评论控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2021-01-05
 */
@Api(description = "笔记评论接口")
@RestController
@RequestMapping("/member/articleReview")
public class ArticleReviewMemberController extends BaseController {

    @Autowired
    private ArticleReviewService articleReviewService;

    @Autowired
    private MemberService memberService;
    /**
     * 分页查询笔记评论
     *
     * @return
    */
    @ApiOperation("分页查询笔记评论")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query")
    })
    public CommResult<ArticleReview> list(Date startDate, Date endDate, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
           params.put("startDate", startDate);
        }
        if (endDate!=null){
           params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }


        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<ArticleReview> list = articleReviewService.selectList(params);
        PageResult<ArticleReview> pageResult = new PageResult<ArticleReview>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条笔记评论
     *
     * @return
    */
    @ApiOperation("查询单条笔记评论")
    @GetMapping("/find/{id}")
    public CommResult<ArticleReview> find(@PathVariable String id) {
        ArticleReview result = articleReviewService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("articleReview",result);
        return CommResult.success(data);

    }

    /**
     * 保存笔记评论
     *
     * @param articleReview
     * @return
     */
    @ApiOperation("保存笔记评论")
    @PostMapping("/save")
    public CommResult<ArticleReview> save(@RequestBody ArticleReview articleReview) {
        Member current = memberService.getCurrent();
        if (current == null) {
            return CommResult.error();
        }
        return CommResult.success(articleReviewService.save(current, articleReview));

    }


    /**
     * 批量删除笔记评论
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除笔记评论", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除笔记评论,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<ArticleReview> del(@PathVariable String ids) {
        if (StringUtils.isBlank(ids)) {
            throw new CustomException("删除失败");
        }
        Member member = memberService.getCurrent();
        String[] idsArr = ids.split(",");
        List<String> idsList = Arrays.asList(idsArr);
        List<Long> memberIds = articleReviewService.selectMemberIdByArticleReviewIds(idsList);
        if (memberIds == null || memberIds.size() > 1 || memberIds.get(0) != member.getId()) {
            throw new CustomException("删除失败");
        }
        articleReviewService.deleteByIds(idsArr);
        return CommResult.success();
    }

    /**
     * 更新笔记评论
     *
     * @param articleReview
     * @return
     */
    @Log(desc = "更新笔记评论", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新笔记评论")
    @PostMapping("/update")
    public CommResult<ArticleReview> update(@RequestBody ArticleReview articleReview) {
        int affectCount = articleReviewService.updateByPrimaryKeySelective(articleReview);
        if (affectCount <= 0){
            return CommResult.error();
        }
        ArticleReview result = articleReviewService.findById(articleReview.getId());
        return CommResult.success(result);
    }

}
